package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.model.JsonData;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import java.util.Date;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Aspire on 2018/3/28.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public ServerResponse login(
        HttpServletRequest request, String username, String password){
        User user=new User();
        user.setId("1");
        user.setUsername("hjd");
        user.setPassword("fhdf");
        user.setEmail("sds@mail.com");
        user.setQuestion("hello");
        user.setRole(0);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        ServerResponse serverResponse = userService.login(username, password);
        request.setAttribute("user", serverResponse.getData());
        return ServerResponse.createSuccess(user);
    }

    @RequestMapping("/register")
    @ResponseBody
    public ServerResponse register(User user){
        user.setRole(0);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        return userService.register(user);
    }
    @RequestMapping("/check_valid")
    @ResponseBody
    public ServerResponse check_valid(@RequestParam("str") String str, @RequestParam("type") String type){
        ServerResponse<User> serverResponse=userService.check_valid(str,type);
        return serverResponse;
    }

    @RequestMapping("/get_user_info")
    @ResponseBody
    public ServerResponse get_user_info(HttpSession session){
      ServerResponse<User> serverResponse=userService.get_user_info(session);
      return serverResponse;
    }

    //@RequestMapping("/")

}
